package com.xtecher.app.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView工具类
 * Created by Luhao on 2017/2/25.
 */

public class RecyclerUtils {

    /**
     * 在使用LinearLayoutManager的情况下判断是否滑到到最后
     *
     * @param re
     * @return
     */
    public static boolean isScrollEnd(RecyclerView re) {
        LinearLayoutManager lm = (LinearLayoutManager) re.getLayoutManager();
        return lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop() == 0 &&
                lm.findFirstVisibleItemPosition() == 0;
    }

    /**
     * 判断RecyclerView是否滑动到底部
     *
     * @param recyclerView
     * @return
     */
    public static boolean isSlideToBottom(RecyclerView recyclerView) {
        return recyclerView != null &&
                recyclerView.computeVerticalScrollExtent() +    //当前屏幕显示的区域高度
                        recyclerView.computeVerticalScrollOffset() >=   //当前屏幕之前滑过的距离
                        recyclerView.computeVerticalScrollRange();   //整个View控件的高度
    }

    /**
     * 在使用LinearLayoutManager的情况下获取RecyclerView的滑动距离
     *
     * @param re
     * @return
     */
    public static int getScollYDistance(RecyclerView re) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) re.getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        int itemHeight = firstVisiableChildView.getHeight();
        return (position) * itemHeight - firstVisiableChildView.getTop();
    }
}
